var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
  data: {
    datalist: [],
		pagenum:1,
		keyword:'',
  },
  _onLoad: function () {
    var that = this
		that.setData({ pagenum: 1, datalist: [], $nomoreData: false});
    $.get('luntan/index', {}, function (res) {
      that.setData(res);
			wx.setNavigationBarTitle({
				title: res.title
			});
    });
    that.getDataList();
  },
  getDataList: function () {
    var that = this
    var pagenum = that.data.pagenum
    var keyword = that.data.keyword
		that.setData({loaddingmore:true})
    $.post('luntan/index', { pagenum: pagenum}, function (res) {
			that.setData({loaddingmore:false})
      var data = res.datalist
      if (pagenum == 1) {
        that.setData({ datalist: data });
        if (data.length == 0) {
          $.showEmpty('暂无相关数据！');
        }
      } else {
        if (data.length == 0) {
          that.setData({ $nomoreData: true });
        } else {
          var datalist = that.data.datalist
          var newdata = datalist.concat(data)
          that.setData({ datalist: newdata });
        }
      }
      that.setData({
        sclist: data
      });
    },false);
  },
  onReachBottom: function () {
    if (!this.data.$dataEmpty.visible && !this.data.$nomoreData) {
      this.setData({ pagenum: this.data.pagenum + 1 })
      this.getDataList();
    }
  },
	zan:function(e){
		var that = this;
		var id = e.currentTarget.dataset.id
		var index = e.currentTarget.dataset.index
		var datalist = that.data.datalist
		$.post("luntan/zan",{id:id},function(res){
			if(res.type==0){ //取消点赞
				datalist[index].iszan = 0;
				datalist[index].zan = datalist[index].zan-1;
			}else{
				datalist[index].iszan = 1;
				datalist[index].zan = datalist[index].zan+1;
			}
			that.setData({
				datalist: datalist
			});
		})
	},
}))